﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OT.Common;
using System.ComponentModel;
using DevExpress.Web.ASPxGridView.Export;
using OT.Dev;

public partial class UseControl_PageCommon : System.Web.UI.UserControl
{
    #region Page_Init

    protected void Page_Init(object sender, EventArgs e)
    {
        AttributeAdd();
    }

    #endregion

    #region Page_Load

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
        if (!IsDetailMode)
        {
            drpDetailMode.Visible = false;
            btnExportAll.Visible = false;
        }
        else
        {
            drpDetailMode.Visible = true;
            btnExportAll.Visible = true;
        }

        // 分页风格
        EnumUtility.ComboBindEnum<PagerStyle>(drpPagerStyle);
        // 列表数据导出文件格式
        EnumUtility.ComboBindEnum<ExportFileType>(drpExportType);
        // 从表导出
        EnumUtility.ComboBindEnum<DetailExportMode>(drpDetailMode);
    }

    #endregion

    #region ReInit

    public void ReInit()
    {
        if (string.IsNullOrEmpty(GridClientName))
            return;

        // 明细表不能单独导出数据
        drpExportType.Visible = false;
        btnExport.Visible = false;
        drpDetailMode.Visible = false;
        chkSelectAll.Visible = false;

        AttributeAdd();
    }

    #endregion

    #region AttributeAdd

    protected void AttributeAdd()
    {
        if (string.IsNullOrEmpty(GridClientName))
            return;

        btnAdd.Attributes.Add("onclick", string.Format("{0}.PerformCallback('New');return false;", GridClientName));
        btnDel.Attributes.Add("onclick", string.Format("{0}.PerformCallback('Delete');return false;", GridClientName));
        btnCustWindow.Attributes.Add("onclick", string.Format("ShowHideCustomizationWindow({0});return false;", GridClientName));
        btnFilterRow.Attributes.Add("onclick", string.Format("UpdateFilterRowVisibility({0});return false;", GridClientName));
        chkSelectAll.Attributes.Add("onclick", string.Format("CheckAllClick({0},this);", GridClientName));
        drpPagerStyle.Attributes.Add("onchange", string.Format("PagerStyleChanged({0},this);", GridClientName));
        drpDetailMode.Attributes.Add("onchange", string.Format("DetailExportModeChanged({0},this);", GridClientName));
        gridExporter.GridViewID = GridClientName;
    }

    #endregion

    /***
     *  Event
     */

    #region btnExport_Click

    protected void btnExport_Click(object sender, EventArgs e)
    {
        gridExporter.ExportedRowType = GridViewExportedRowType.Selected;
        // 将表格数据导出
        OnExport(new EventArgs());
    }

    #endregion

    #region btnExportAll_Click

    protected void btnExportAll_Click(object sender, EventArgs e)
    {
        gridExporter.ExportedRowType = GridViewExportedRowType.All;
        // 将表格数据导出
        OnExport(new EventArgs());
    }

    #endregion

    #region Export
    private static readonly object EventQueryKey = new object();

    [
        Category("Action"),
        Description("文件导出")
    ]
    public event EventHandler Export
    {
        add
        {
            Events.AddHandler(EventQueryKey, value);
        }
        remove
        {
            Events.RemoveHandler(EventQueryKey, value);
        }
    }

    protected virtual void OnExport(EventArgs e)
    {
        EventHandler ExportHandler = (EventHandler)Events[EventQueryKey];

        if (ExportHandler != null)
        {
            ExportHandler(this, e);
        }
    }
    #endregion

    #region FileType

    public ExportFileType FileType
    {
        get
        {
            return EnumUtility.GetEnumValue<ExportFileType>(drpExportType.SelectedValue);
        }
    }
    #endregion

    #region PagerType

    public PagerStyle PagerType
    {
        get
        {
            return EnumUtility.GetEnumValue<PagerStyle>(drpPagerStyle.SelectedValue);
        }
    }
    #endregion

    #region GridExpoter

    public DevExpress.Web.ASPxGridView.Export.ASPxGridViewExporter GridExpoter
    {
        get
        {
            return gridExporter;
        }
    }
    #endregion

    #region DisableAdd

    public void DisableAdd()
    {
        btnAdd.Enabled = false;
    }
    #endregion

    #region DisableDelete

    public void DisableDelete()
    {
        btnDel.Enabled = false;
    }
    #endregion

    #region DisablePagerStyle

    public void DisablePagerStyle()
    {
        drpPagerStyle.Enabled = false;
    }
    #endregion

    #region GridClientName

    public string GridClientName
    {
        get
        {
            string s = (string)ViewState["GridClientName"];
            return ((s == null) ? string.Empty : s);
        }
        set
        {
            ViewState["GridClientName"] = value;
        }
    }
    #endregion

    #region IsDetailMode

    public bool IsDetailMode
    {
        get
        {
            return ((ViewState["IsDetailMode"] == null) ? false : Convert.ToBoolean(ViewState["IsDetailMode"]));
        }
        set
        {
            ViewState["IsDetailMode"] = value;
        }
    }
    #endregion

}
